package tp1.dao;
import com.mysql.jdbc.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import tp1.models.Cliente;
import tp1.DbConnection;

/**
 * Implementação DAO usando JDBC
 * @author tulio
 */
public class ClienteDao {

    public static int delete(String cpf) throws SQLException {
        Statement stmt = (Statement) DbConnection.getInstance().createStatement();
        return stmt.executeUpdate("DELETE FROM CLIENTE WHERE CPF = '" + cpf + "'");
    }

    public static Cliente find(String cpf) throws SQLException {
        Cliente c = null;
        Statement stmt = (Statement) DbConnection.getInstance().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM CLIENTE WHERE CPF = '" + cpf + "'");

        while(rs.next()){
            c = getClienteByResultSet(rs, null);
        }        
        return c;
    }

    public static ArrayList<Cliente> list() throws SQLException {
        Cliente c;
        ArrayList<Cliente> list = new ArrayList();
        Statement stmt = (Statement) DbConnection.getInstance().createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM CLIENTE");

        while(rs.next()){
            list.add(getClienteByResultSet(rs, null));
        }
        
        return list;
    }

    public static int insert(Cliente cliente) throws SQLException {
        String insert = "INSERT INTO CLIENTE (CPF, NOME, ENDERECO) VALUES('" + cliente.getCpf() + "','" + cliente.getNome() + "','" + cliente.getEndereco() + "')";
        Statement stmt = (Statement) DbConnection.getInstance().createStatement();
        return stmt.executeUpdate(insert);
    }

    public static int update(Cliente cliente) throws SQLException {
        String update = "UPDATE CLIENTE SET NOME = '" + cliente.getNome() + "', ENDERECO = '" + cliente.getEndereco() + "' WHERE CPF = '" + cliente.getCpf() + "'";
        Statement stmt = (Statement) DbConnection.getInstance().createStatement();
        return stmt.executeUpdate(update);
    }

    public static Cliente getClienteByResultSet(ResultSet rs, String alias) throws SQLException{
        if(alias == null) alias = "CLIENTE";
        Cliente c = new Cliente(rs.getString(alias + ".CPF"));
        c.setNome(rs.getString(alias + ".NOME"));
        c.setEndereco(rs.getString(alias + ".ENDERECO"));
        return c;
    }

    public static int save(Cliente cliente) throws SQLException{
        int result;
        if(find(cliente.getCpf()) == null){
            result = insert(cliente);
        } else {
            result = update(cliente);
        }                    
        return result;
    }

    public static Object[][] listAsVector() throws SQLException{
        int row = 0;
        ArrayList<Cliente> clientes = ClienteDao.list();
        Object[][] rows;
        
        if(clientes.size() == 0){
            rows = null;
        } else {
            rows = new Object[clientes.size()][3];
        }
        
        for (Cliente c : clientes) {
            rows[row][0] = c.getCpf();
            rows[row][1] = c.getNome();
            rows[row][2] = c.getEndereco();
            row++;
        }
        return rows;
    }

    public static Object[][] findAsVector(String cpf) throws SQLException{
        Cliente cliente = ClienteDao.find(cpf);
        Object[][] rows = null;
        if(cliente != null){
            rows = new Object[1][3];
            rows[0][0] = cliente.getCpf();
            rows[0][1] = cliente.getNome();
            rows[0][2] = cliente.getEndereco();
        }
        return rows;
    }

}
